<template>
	<view class="container999">
		<!-- #ifdef MP-TOUTIAO  -->
		<navTopTT title="设置" titleColor="black" backgroundColor="white"></navTopTT>
		<!-- #endif -->
		<!-- #ifdef MP-KUAISHOU  -->
		<navTop title="设置" titleColor="black" backgroundColor="white"></navTop>
		<!-- #endif -->
		<!-- <view v-if="isLogin" class="line">
			<button open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">
				<text>绑定手机</text>
			</button>
		</view> -->
		<view v-if="isLogin" class="line">
			<view @click="loginout" class="lineLeft">
				<image style="width: 30rpx; height: 30rpx; margin-right: 10rpx" src="https://res.qiguoread.com/staticResources/fellow_townsman/image/icon_set.png"></image>
				<text>退出登录</text>
			</view>
		</view>
		<!-- #ifdef MP-KUAISHOU -->
		<view v-else>
			<button @tap="postLogin" class="btn-set">登录</button>
		</view>
		<!-- #endif -->
		<view style="opacity: 0;" class="line">
			<view @longpress="toTest" class="lineLeft">
				<image style="width: 30rpx; height: 30rpx; margin-right: 10rpx" src="https://res.qiguoread.com/staticResources/fellow_townsman/image/icon_set.png"></image>
				<text>TEST</text>
			</view>
		</view>
		<view @dblclick="handleJumpTest" class="version">V{{ version }}</view>
	</view>
</template>

<script>
const util = require('../../util/util.js');

export default {
	data() {
		return {
			isLogin: uni.getStorageSync('alreadyAuthLogin') ? true : false,
			version: '1.0.13'
		};
	},
	onLoad() {
		//#ifdef MP-KUAISHOU
		const accountInfo = uni.getAccountInfoSync();
		if (accountInfo && accountInfo.miniProgram && accountInfo.miniProgram.version) this.version = accountInfo.miniProgram.version;
		//#endif

		//#ifdef MP-TOUTIAO
		if(tt.getEnvInfoSync){
			const accountInfo = tt.getEnvInfoSync();
			if (accountInfo && accountInfo.plugin && accountInfo.plugin.version) this.version = accountInfo.plugin.version;
			console.log('accountInfo', accountInfo);
		}
		//#endif
		this.getUserInfoMess()
	},
	methods: {
		// 跳转分销商测试页
		handleJumpTest() {
			uni.navigateTo({
				url: '../toTest/toTestTwo'
			})
		},
		async getPhoneNumber (e) {
			try {
				if (!e.detail.iv){
					uni.toast({
						txt: '授权失败！',
						icon: 'none'
					});
					return;
				}	

				uni.login({
					success: async (res) => {
						await this.zwyRequest({
							url: '/user/mobile_login/binding_kuai_shou_mobile',
							method: 'PUT',
							data: {
								jsCode: res.code,
								data: e.detail.encryptedData,
								iv: e.detail.iv
							}
						})
					}
				})
			} catch (error) {
				console.log(error)
			}
		},
		toTest() {
			console.log('toTest');
			uni.navigateTo({
				url: '../toTest/toTest'
			});
		},
		postLogin: util.throttle(async function() {
			if (this.isLogin) return;

			//#ifdef MP-KUAISHOU
			uni.getUserInfo({
				success: res => {
					console.log('postLogin', res);
					this.updateInfo(res.userInfo.nickName, res.userInfo.avatarUrl);
				}
			});
			//#endif
		}, 2000),
		updateInfo(nickName, avatarUrl) {
			uni.showLoading({
				title: '登录中',
				mask: true
			});
			let postUrl = '/user/login_user/update_kuaishou_user_info';
			
			//#ifdef MP-TOUTIAO
				postUrl = '/user/login_user/update_douyin_user_info'
			//#endif
			
			// this.userInfo = res.userInfo;
			this.zwyRequest({
				url: postUrl,
				method: 'POST',
				data: {
					nickName: nickName,
					iconUrl: avatarUrl
				}
			}).then(resUpdate => {
				console.log('postLogin', resUpdate);
				this.zwyLogin().then(resLogin => {
					console.log('zwyLogin', resLogin);
					setTimeout(() => {
						
						uni.hideLoading();
						this.userInfo = uni.getStorageSync('userInfo') || {};
						uni.setStorageSync('alreadyAuthLogin', true);
						uni.reLaunch({
							url: '/pages/mine/mine'
						})
						this.isLogin = true;
					}, 500);
				});
			});
		},
		loginout() {
			console.log('loginout');
			this.isLogin = false;
			uni.setStorageSync('alreadyAuthLogin', false);
			this.userInfo = {};
		},
		async getUserInfoMess() {
			try {
				let res = await this.zwyRequest({
					url: `/user/login_user/info`,
					method: 'GET',
					params: {
						isUser: true
					}
				})

				console.log(res)
			} catch (error) {
				console.log(error)
			}
		}
	}
};
</script>

<style lang="scss">
.lineLeft {
	display: flex;
	align-items: center;
	justify-content: center;
	margin-left: 50rpx;
}
.line {
	border-bottom: 1rpx solid rgba(221, 221, 221, 1);
	width: 100%;
	font-size: 28rpx;
	display: flex;
	align-items: center;
	justify-content: space-between;
	height: 80rpx;
	line-height: 80rpx;
}
.version {
	width: 100vw;
	height: 80rpx;
	line-height: 80rpx;
	text-align: center;
	color: #ccc;
	font-weight: bold;
	position: absolute;
	left: 0;
	bottom: 30rpx;
}

.btn-set {
	background: linear-gradient(339deg,#f36d86 0%,#ff70a2 100%);
	color: white;
	margin-top: 128rpx;
}
</style>
